<?php
	/**
	* Product Edit
	* Edit action
	* @author trungdt
	* @since Sep 27, 2011 
	* 
	* Change logs
	* 
	*/

	if (!defined('EXPONENT')) exit('');

	$product = null;
	$config=null;
	if (isset($_GET['id'])) {
		$product = $db->selectObject('productsmodule_products','id='.intval($_GET['id']));
		$config=null;
		if ($product) {
			$loc = unserialize($product->location_data);
		}
	}
	else
	{
		// here, we should inital the $product object
		$product->title = '';
		$product->seo_name='';
		$product->search_des = '';
		$product->cat_id = productsmodule::GetFirstCategoryId();
		$product->post_date=time();
		$product->og_thumbnail=0; // og thumbnail image file path
		$product->best_seller=0;
		$product->images="";
		$product->description="";
		$product->spec="";
		$product->brand_id=0;
		$product->sku=0;
		$product->rfid=0;
		$product->price=0;
		$product->price_orginal=0;
		$product->product_state=0;
		$product->user_id=$user->id;
		$product->videos="";
		$product->stars=0;
		$product->product_state2=0;
		$product->on_sale=0;
		$product->hot_product=0;
		$product->stock_remain=0;
		$product->accessories="";
		$product->count_view=0;
		$product->public_state=0;
		$product->weight=0;
	}

	$config = $db->selectObject('productsmodule_config',"location_data='".serialize($loc)."'");

	if ($config == null) {
		$params=array();
		$params['module']=$loc->mod;
		$params['action']='configure';
		$params['src']=$loc->src;
		$params['_common']=1;
		$product=exponent_core_makeLink($params);
		header("Location: {$product}");
		die();
	}

	if (exponent_permissions_check(array('administrate','configure', "edit", "create"),$loc)) {
		//$form = productsmodule_categories::product_form($product,$config);
		//$form->location($loc);
		//$form->meta('action','product_save');

		// passing param to build form
		$lang=exponent_lang_loadFile("modules/productsmodule/common.php");
		$admin_permission=false;
		if (exponent_permissions_check(array('administrate','configure'), $loc))
			$admin_permission=true;
		
		// product state
		$product_state=array();
		for ($i=0;$i<$lang['product_state_count'];$i++)
		{
			$product_state[$i]=$lang["product_state{$i}"];
		}

		// product state2
		$product_state2=array();
		for ($i=0;$i<$lang['product_state_2_count'];$i++)
		{
			$product_state2[$i]=$lang["product_state_2_{$i}"];
		}
		
		// public state
		$public_state=array();
		for ($i=0;$i<$lang['product_public_state_count'];$i++)
		{
			$public_state[$i]=$lang["product_public_state_{$i}"];
		}
		
		// for image list
		$images=explode(";",$product->images);
		$sql_where="(0>1) ";
		foreach ($images as $v)
			if (trim($v) != "")
				$sql_where.=" OR (id = {$v})";
		$images=$db->selectObjects("file", $sql_where);
		foreach ($images as $k=>$v){
			$v->path=BASE_URL."/".$v->directory."/".$v->filename;
		}
		
		// for product thumbnail
		if (isset($product->og_thumbnail) && ($product->og_thumbnail > 0))
		{
			$file = $db->selectObject('file', 'id='.$product->og_thumbnail);
			$product->og_thumbnail_filepath = URL_FULL.$file->directory.'/'.$file->filename;
		}
		else
			$product->og_thumbnail_filepath="";
		
		// brand
		$brands=$db->selectObjects("productsmodule_brand", "location_data = '".serialize($loc)."'");
		
		$template = new template('productsmodule','_product_edit', $loc);
		$template->register_permissions(array('administrate', "configure", "create", "edit", "delete"),$loc);
		$template->assign('is_edit',(isset($product->id)? 1 : 0));
		$template->assign('product',$product);
		$template->assign('admin_permission',$admin_permission);
		$template->assign('product_state2',$product_state2);
		$template->assign('product_state',$product_state);
		$template->assign('images_list',$images);
		$template->assign('brands',$brands);
		$template->assign('public_state',$public_state);
		$template->assign('prev_url',exponent_flow_get());

		//$template->assign('form_html',$form->toHTML());
		$template->output();
	} else {
		echo SITE_404_HTML;
	}

?>